package com.y.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.y.entity.Order;
import com.y.entity.vo.DataTable;
import com.y.entity.vo.R;
import com.y.service.OrderService;
import com.y.utils.ConstantName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yjh
 * @since 2021-09-24
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;


    @PostMapping("/createOrder")
    @ResponseBody
    public R createOrder(@RequestParam(name = "fId")Integer fId,
                         @RequestParam(name = "nums")Integer nums){
        return  orderService.addOrder(fId, nums);
    }


    @GetMapping("/showUserOrder")
    public String show(){
        return "front/showOrder";
    }


    @PostMapping("/showUserOrder")
    @ResponseBody
    public DataTable<Order> showUserOrder(@RequestParam(value = "page",defaultValue = "1") int page,
                                          @RequestParam(value = "limit",defaultValue = "10") int limit){
        List<Order> list = orderService.showUserOrderByStatus(ConstantName.ORDER_CART_STATUS);
        return new DataTable<>(list.size(),list);
//        List<Order> orderList = list.subList((page - 1) * limit, (page - 1) * limit + limit);
//        return new DataTable<>(orderList.size(),orderList);

    }



    @GetMapping("/showUserBuyOrder")
    public String showBuy(){
        return "front/showBuyOrder";
    }



    @PostMapping("/showUserBuyOrder")
    @ResponseBody
    public DataTable<Order> showUserOrderBuy(@RequestParam(value = "page",defaultValue = "1") int page,
                                          @RequestParam(value = "limit",defaultValue = "10") int limit,
                                          @RequestParam(value = "name",required = false) String name){
        List<Order> list = orderService.showUserOrderByStatus(ConstantName.ORDER_BUYING_STATUS);
        return new DataTable<>(list.size(),list);
//        List<Order> orderList = list.subList((page - 1) * limit, (page - 1) * limit + limit);
//        return new DataTable<>(orderList.size(),orderList);

    }

    /**
     * 查看单个订单 订单ID
     * @return
     */
    @RequestMapping("/getOneOrder/{id}")
    public String getOneOrder(@PathVariable(name = "id")Integer id,Model model){
        Order order = orderService.getOneOrderByOID(id);
        model.addAttribute("order",order);
        return "view/system/operate/order/buyOrder";
    }


    /**
     * 购买商品
     */
    @PostMapping("/buyOrder")
    @ResponseBody
    public R buyOrder(@RequestParam(name = "oId")Integer oId){
        return orderService.buyOrder(oId);
    }

    /**
     * 从购物车中删除商品
     */
    @DeleteMapping("/removeOrder/{id}")
    @ResponseBody
    public R removeOrder(@PathVariable(name = "id")Integer id){
        return orderService.removeOrder(id);
    }

    /**
     * 从购物车中编辑商品
     */
    @GetMapping("/editOrder/{id}")
    public String editOrderPage(@PathVariable(name = "id")Integer oId,Model model){
        Order order = orderService.getOneOrderByOID(oId);
        model.addAttribute("order",order);
        return "view/system/operate/order/editOrder";
    }

    /**
     * 更新编辑商品
     */
    @PostMapping("/editOrder")
    @ResponseBody
    public R editOrder(@RequestParam(name = "oId")Integer oId,
                            @RequestParam(name = "nums")Integer nums){
        return orderService.editOrderUpdate(oId,nums);
    }
}
